////#include <iostream>
////
////using namespace std;
////
////int main(int argc, char* argv[])
////{
////	int n;
////	cin>>n;
////	while (n != 0) {
////		int *A = new int[n];
////		int i = 0;
////		while (i < n) {
////			int ai;
////			cin>>ai;
////			A[i++] = ai;
////		}
////
////		bool isBack = false;
////		int maxSum = 0, secMaxSum = 0, sumOfSubSeq = 0;
////		int minIdx = 0, maxIdx = n -1;
////		bool isFirst = false;
////		int firstIdx = -1;
////		int reallyFirst = -1;
////		int numberSeq = 0;
////
////		i = 0;
////		do{
////			if (A[i] < 0) {
////				if (!isFirst) {
////					firstIdx = i;
////					isFirst = true;
////					reallyFirst = i;
////				} else {
////					++numberSeq;
////
////					sumOfSubSeq = 0;
////					int j = (firstIdx + 1) % n;
////					minIdx = j;
////					do {
////						sumOfSubSeq += (A[j]);
////						++j;
////						j %= n;
////					}while (j != i);
////					
////					isFirst = true;
////					firstIdx = i;
////
////					if (maxSum < sumOfSubSeq) {
////						if (i == 0) 
////							maxIdx = n -1;
////						else
////							maxIdx = i - 1;
////						if (numberSeq > 1) {
////							secMaxSum = maxSum;
////						}
////						maxSum = sumOfSubSeq;						
////					} else {
////						if (numberSeq > 1) {
////							if (secMaxSum < sumOfSubSeq)
////								secMaxSum = sumOfSubSeq;
////						}
////					}
////
////									
////				}
////			}
////			++i;
////			i %= n;
////			if (i == 0) {
////				if (reallyFirst == -1)
////					break;
////			}  
////			if (i == (reallyFirst + 1) % n) {
////				if (!isBack) 
////					isBack = true;
////				else
////					break;
////			}
////		} while (true);
////
////		if (reallyFirst == -1) {
////			maxSum = 0;
////			secMaxSum = 0;
////			for (i = 0; i < n; i++) {
////				maxSum += (A[i]);
////			}
////		}
////		cout << maxSum + secMaxSum<<endl;
////		delete[] A;
////		cin>>n;
////	}
////	
////}
//
//#include <iostream>
//
//using namespace std;
//
//int main(int argc, char* argv[])
//{
//	int n;
//	cin>>n;
//	while (n != 0) {
//		int *A = new int[n];
//		int i = 0;
//		while (i < n) {
//			int ai;
//			cin>>ai;
//			A[i++] = ai;
//		}
//
//		int maxSum = 0, secMaxSum = 0, sumOfSubSeq = 0;		
//		bool isFirst = false;
//		int firstIdx = -1;		
//		int numberSeq = 0;
//		int reallyFirst = -1;
//
//		i = 0;
//		do{
//			if (A[i] < 0) {
//				if (!isFirst) {
//					firstIdx = i;
//					isFirst = true;
//					reallyFirst = i;
//				} else {
//					++numberSeq;
//					sumOfSubSeq = 0;
//					int j = firstIdx + 1;
//					if (j >= n) 
//						break;
//					do {
//						sumOfSubSeq += A[j];
//						++j;
//					}while (j < i);
//					
//					isFirst = true;
//					firstIdx = i;
//
//					if (maxSum < sumOfSubSeq) {
//						if (numberSeq > 1) {
//							secMaxSum = maxSum;
//						}
//						maxSum = sumOfSubSeq;						
//					} else {
//						if (numberSeq > 1) {
//							if (secMaxSum < sumOfSubSeq)
//								secMaxSum = sumOfSubSeq;
//						}
//					}
//				}
//			}
//			++i;
//		} while (i < n);
//
//		
//
//		if (firstIdx == -1) {
//			maxSum = 0;
//			secMaxSum = 0;
//			for (i = 0; i < n; i++) {
//				maxSum += A[i];
//			}
//		} else {
//			if (firstIdx + 1 < n) {
//				++numberSeq;
//				sumOfSubSeq = 0;
//				for (i = firstIdx + 1; i < n; i++) {
//					sumOfSubSeq += A[i];
//				}
//				if (maxSum < sumOfSubSeq) {
//					if (numberSeq > 1) {
//						secMaxSum = maxSum;
//					}
//					maxSum = sumOfSubSeq;						
//				} else {
//					if (numberSeq > 1) {
//						if (secMaxSum < sumOfSubSeq)
//							secMaxSum = sumOfSubSeq;
//					}
//				}
//			}
//
//			if (reallyFirst != -1) {
//				++numberSeq;
//				sumOfSubSeq = 0;
//				for (i = 0; i < reallyFirst; i++) {
//					sumOfSubSeq += A[i];
//				}
//				if (maxSum < sumOfSubSeq) {
//					if (numberSeq > 1) {
//						secMaxSum = maxSum;
//					}
//					maxSum = sumOfSubSeq;						
//				} else {
//					if (numberSeq > 1) {
//						if (secMaxSum < sumOfSubSeq)
//							secMaxSum = sumOfSubSeq;
//					}
//				}
//			}
//		}
//		cout << maxSum + secMaxSum<<endl;
//		delete[] A;
//		cin>>n;
//	}
//	
//}

#include <iostream>

using namespace std;

int main(int argc, char *args[])
{
	//const int MAX = 50000;
	//int A[MAX];
	int n;
	scanf("%d", &n);
	while (n != 0) {
		int *A = new int[n];
		for (int j = 0; j < n; j++) {
			scanf("%d", A + j);
		}
		//int **arr = new int*[n];
		//for (int j = 0; j < n; j++) {
		//	arr[j] = new int[n];
		//}
		//for (int j = 0; j < n ; j++) {
		//	arr[j][j] = A[j];
		//}

		//int max = -500000000;
		//for (int step = 1; step < n ; step++) {
		//	for (int j = 0; j + step < n; j += step) {
		//		int k = j + step;
		//		max = -500000000;
		//		int sum = 0;
		//		for (int l = j; l < k; l++) {
		//			sum = (arr[j][l] + arr[l + 1][k]);
		//			sum = sum > arr[j][l] ? sum : arr[j][l];
		//			sum = sum > arr[l + 1][k] ? sum : arr[l + 1][k];
		//			if (sum > max) 
		//				max = sum;
		//		}

		//		arr[j][k] = max;
		//		printf("%d %d %d\n", j, k, max);
		//	}
		//}

		//printf("%d\n", arr[0][n - 1]);
		int *forward = new int[n];
		int *backward = new int[n];
		
		int maxSum = forward[0] = A[0];
		int totalSum = A[0];
		for (int i = 1; i < n; i++) {
			/*totalSum += A[i];
			if (maxSum < totalSum) {
				maxSum = totalSum;
			}
			forward[i] = maxSum;*/
			int preTotalSum = totalSum;
			if (totalSum < 0) {
				totalSum = totalSum < A[i] ? A[i] : totalSum;
			} else {
				totalSum += A[i];
			}
			if (preTotalSum > totalSum) { // The sum goes down
				forward[i] = maxSum;
			} else {
				if (maxSum < totalSum) {
					maxSum = totalSum;
				}
				forward[i] = maxSum;
			}
			//printf("%d\n", forward[i]);
		}

		maxSum = backward[n - 1] = A[n - 1];
		totalSum = A[n - 1];
		for (int i = n - 2; i >= 0; --i) {
			//if ((totalSum + A[i]) < totalSum) { // The sum goes down
			//	backward[i] = maxSum;
			//} else {
			//	if (maxSum < (totalSum + A[i])) {
			//		maxSum = totalSum + A[i];
			//	}
			//	backward[i] = maxSum;
			//}
			//totalSum += A[i];

			int preTotalSum = totalSum;
			if (totalSum < 0) {
				totalSum = totalSum < A[i] ? A[i] : totalSum;
			} else {
				totalSum += A[i];
			}
			if (preTotalSum > totalSum) { // The sum goes down
				backward[i] = maxSum;
			} else {
				if (maxSum < totalSum) {
					maxSum = totalSum;
				}
				backward[i] = maxSum;
			}
			//printf("%d\n", backward[i]);
		}

		
		maxSum = forward[0] + backward[1];
		for (int i = 1; i < n - 1; i++) {
			if (maxSum < (forward[i] + backward[i + 1]))
				maxSum = forward[i] + backward[i + 1];
		}
		printf("%d\n", maxSum);
		//for (int j = 0; j < n; j++) {
		//	delete[] arr[j];
		//}
		//delete[] arr;

		delete[] forward;
		delete[] backward;
		delete[] A;

		scanf("%d", &n);
	}
	return 0;
}